import 'package:bxultimate/domain/common/result.dart';
import 'package:bxultimate/shared/constant/app_constant.dart';
import 'package:bxultimate/shared/util/util_validate.dart';
import 'package:get/get.dart';
import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/request/user_request.dart';
import 'package:bxultimate/data/store/auth_store.dart';
import 'package:bxultimate/domain/auth/login_token.dart';
import 'package:bxultimate/shared/util/util_mk.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'state.dart';

class AuthForgotPwdController extends GetxController {
  final AuthForgotPwdState state = AuthForgotPwdState();

  final TextEditingController phoneController = TextEditingController();
  final TextEditingController codeController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();
  final TextEditingController rePasswordController = TextEditingController();
  final FocusNode nodeText1 = FocusNode();
  final FocusNode nodeText2 = FocusNode();
  final FocusNode nodeText3 = FocusNode();
  final FocusNode nodeText4 = FocusNode();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    phoneController.addListener(_verify);
    codeController.addListener(_verify);
  }

  Future<void> foundPwd() async {
    if (passwordController.text != rePasswordController.text) {
      SmartDialog.showToast('两次密码输入不一致');
      return;
    }

    SmartDialog.showLoading(msg: '提交中...');
    Result result = await UserRequest.forgotPwd(
        phoneController.text, codeController.text, passwordController.text);

    SmartDialog.dismiss();
    if (result.code == AppConstant.RESULT_ERROR) {
      SmartDialog.showToast(result.message!);
      return;
    }

    SmartDialog.showNotify(msg: '密码找回成功', notifyType: NotifyType.success);
    Get.back();
  }

  void _verify() {
    final String name = phoneController.text;
    final String code = codeController.text;
    final String password = passwordController.text;
    final String rePassword = rePasswordController.text;
    if (name.isEmpty || name.length < 11) {
      state.clickable = false;
    } else if (code.isEmpty || code.length < 4) {
      state.clickable = false;
    } else if (password.isEmpty || password.length < 4) {
      state.clickable = false;
    } else if (rePassword.isEmpty || rePassword.length < 4) {
      state.clickable = false;
    } else {
      state.clickable = true;
    }
  }

  void toSmsLogin() {
    Get.toNamed(RouterName.userLoginSms);
  }

  void wxLogin() {}

  Future<bool> getVCode() async {
    if (!UtilValidate.checkPhone(phoneController.text)) {
      SmartDialog.showToast('请输入正确的手机号');
      return false;
    }
    Result result = await UserRequest.sendForgotPwdCode(phoneController.text);
    if (result.code == AppConstant.RESULT_ERROR) {
      SmartDialog.showToast(result.message!);
      return false;
    } else {
      SmartDialog.showToast('验证码已发送');
      return true;
    }
  }
}
